package com.lizemin.beanfactorypostprocessor.processors;

import com.lizemin.beanfactorypostprocessor.entity.Dog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author lzm
 * @date 2025/9/27 11:07
 * @description
 */
@Component
@Slf4j
public class ChangeBeanInstanceProcessProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (!"dog".equals(beanName)) {
            log.info("【{}】经过了bean实例处理器-before init", beanName);
            return bean;
        }
        Dog dog = (Dog) bean;
        log.info("填充了属性的dog的属性值为 name={},color={}", dog.getName(), dog.getColor());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (!"dog".equals(beanName)) {
            log.info("【{}】经过了bean实例处理器-after init", beanName);
            return bean;
        }
        log.info("开始给狗添加年龄");
        Dog dog = (Dog) bean;
        dog.setAge(12);
        return bean;
    }

}
